package vashion.azeroth.core.manager.item;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import vashion.azeroth.contant.AzerothConstants;
import vashion.azeroth.contant.GenericErrorEnum;
import vashion.azeroth.core.dao.BaseDao;
import vashion.azeroth.core.item.dao.ItemLenuxForestDao;
import vashion.azeroth.core.item.pojo.ItemLenuxForestDO;
import vashion.azeroth.core.manager.impl.BaseManagerImpl;
import vashion.azeroth.core.result.ApiResult;

/**
 * Created by Loki on 17/4/26. lenux类目管理
 */
@Service
public class ItemLenuxForestManager extends BaseManagerImpl<ItemLenuxForestDO> {

	private final static Logger log = LoggerFactory.getLogger(ItemLenuxForestManager.class);

	@Autowired
	private ItemLenuxForestDao itemLenuxForestDao;

	@Override
	public BaseDao<ItemLenuxForestDO> getDao() {
		return itemLenuxForestDao;
	}

	/**
	 * 按照lenux类目名精确查找类目类
	 * 
	 * @param lenuxForestTitle
	 * @return lenux类目名为lenuxForestTitle的类目类
	 */
	public ApiResult<List<ItemLenuxForestDO>> findByLenuxCataTitle(String lenuxForestTitle) {
		try {
			List<ItemLenuxForestDO> itemLenuxForestList = itemLenuxForestDao.findByLenuxCataTitle(lenuxForestTitle);
			return ApiResult.newSuccessResult(itemLenuxForestList);
		} catch (Exception e) {
			return ApiResult.newErrorResult(GenericErrorEnum.SYSTEM_ERROR.getErrorCode(),
					GenericErrorEnum.SYSTEM_ERROR.getErrorMessage());
		}
	}

	/**
	 * 保存lenux类目和本系统的类目的关联
	 * 
	 * @param itemLenuxForestDO
	 * @return lenux类目和系统类目关联保存结果
	 */
	public ApiResult<Boolean> publishLenuxForest(ItemLenuxForestDO itemLenuxForestDO) {
		ApiResult<List<ItemLenuxForestDO>> lenuxForestResult = this
				.findByLenuxCataTitle(itemLenuxForestDO.getLenuxCataTitle());
		if (null == itemLenuxForestDO.getFeature()
				|| (!itemLenuxForestDO.getFeature().equals(AzerothConstants.Item.MALE.toString())
						&& !itemLenuxForestDO.getFeature().equals(AzerothConstants.Item.FEMALE.toString())
						&& !itemLenuxForestDO.getFeature().equals(AzerothConstants.Item.ALL.toString()))) {
			itemLenuxForestDO.setFeature(AzerothConstants.Item.ALL.toString());
		}
		try {
			if (lenuxForestResult.isSuccess()) {
				if (null == lenuxForestResult.getData() || lenuxForestResult.getData().isEmpty()) {
					itemLenuxForestDao.save(itemLenuxForestDO);
					return ApiResult.newSuccessResult(true);
				} else {
					boolean hasSameFeature = false;
					for (ItemLenuxForestDO itemLenuxForestTemp : lenuxForestResult.getData()) {
						if(itemLenuxForestDO.getFeature().equals(itemLenuxForestTemp.getFeature())) {
							hasSameFeature = true;
							break;
						}
					}
					if (hasSameFeature) {
					return ApiResult.newSuccessResult(false);
					} else {
						itemLenuxForestDao.save(itemLenuxForestDO);
						return ApiResult.newSuccessResult(true);
					}
				}
			} else {
				return ApiResult.newErrorResult(GenericErrorEnum.SYSTEM_ERROR.getErrorCode(),
						GenericErrorEnum.SYSTEM_ERROR.getErrorMessage());
			}
		} catch (Exception e) {
			return ApiResult.newErrorResult(GenericErrorEnum.SYSTEM_ERROR.getErrorCode(),
					GenericErrorEnum.SYSTEM_ERROR.getErrorMessage());
		}
	}

}
